package com.livepan.server.utils;

import java.io.File;
import java.io.IOException;

public class FileUtil {

    //本地对于头像的临时存储文件夹
    public static final String avatarpath = getRootPath() + "/temp/images/avatar/";

    /**
     * 得到我存储文件的根目录，
     * linux放在/www/wwwroot/question/
     * windows放在D:/run/
     */
    public static synchronized String getRootPath() {
        if (isWindows()) {
            return "D:/me/run/question/";
        } else {
            return "/www/wwwroot/question/";
        }
    }

    public static void checkDirPath(String dirpath) {
        File dir = new File(dirpath);
        checkDir(dir);
    }

    public static void checkDir(File dir) {
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }


    public static void checkFilePath(String filepath) throws IOException {
        File file = new File(filepath);
        checkFile(file);
    }

    public static void checkFile(File file) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    public static boolean isLinux() {

        return System.getProperty("os.name").toLowerCase().contains("linux");

    }

    public static boolean isWindows() {

        return System.getProperty("os.name").toLowerCase().contains("windows");

    }
}
